#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def Search_CaseSensitive_Return(regex, line):
    import re
    pattern = re.compile(regex)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Search_CaseIgnore_Return(regex, line):
    import re
    pattern = re.compile(regex, re.I)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Search_Symbol(regex, line):
    import re
    pattern = re.compile(regex, re.I)
    m = re.search(pattern, line)
    if m:
        return m.group(2)
    else:
        return line


if __name__ == "__main__":
    # from pymongo import MongoClient
    import re

    import sys

    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
        reload(sys)
        sys.setdefaultencoding(default_encoding)

    # DB_NAME = "organization"
    # COLLECTION_NAME = "organization"
    #
    # connection = MongoClient('localhost', 27017)
    # db = connection[DB_NAME]
    # collection = db[COLLECTION_NAME]
    #
    # regex_label = "<.*?>"
    # regex_sup = "<sup>.*?</sup>"
    # regex_blank = " +"

    regex_arr = []
    type_arr = []
    infile_org_type = open("D:/org_type_wos", "r")
    for line in infile_org_type:
        line = line.rstrip()
        splitline = line.split("\t")
        type_arr.append(splitline[0])
        # regex_arr.append("[,;]? *([^,;]*" + splitline[1] + "[^,\.;]*)[,\.;]?")
        regex_arr.append("^(.*?"+splitline[1]+".*?),")
    infile_org_type.close()

    # infile_symbol = open("symbol", "r")
    # regex_s = infile_symbol.readline().rstrip()
    # infile_symbol.close()
    # regex_symbol = "^" + regex_s + "(.*?)" + regex_s + "$"

    infile=open("D:/data/wos_org_list.txt","r")
    outfile=open("D:/data/wos_org_list_with_org_type","w")

    ht={}

    infile.readline()
    count=0
    for line in infile:
        line=line.rstrip("\n")
        splitline=line.split("\t")
        org=splitline[0][1:-1]

    # results = collection.find({"country": "China"})
    # for result in results:
    #     org = result["org"]
    #     id = result["id"]
        # org2 = re.sub(regex_sup, "", org, 0, re.I)
        # org2 = re.sub(regex_label, "", org2, 0, re.I)
        # org2 = re.sub(regex_blank, " ", org2, 0, re.I)
        line_found = None
        for i in xrange(0, len(regex_arr)):
            found = Search_CaseIgnore_Return(regex_arr[i], org)
            if found != None:
                line_found = found
                # line_found = Search_Symbol(regex_symbol, line_found)
                # collection.update({"id": id}, {"$set": {"uni_org": line_found, "uni_org_type": type_arr[i]}})
                # print "======================================"
                # print line_found
                # print type_arr[i]
                # print "======================================"
                ht[line_found]=0
                outfile.write(line+"\t\""+line_found+"\"\t"+type_arr[i]+"\n")
                outfile.flush()
                break
        if line_found == None:
            line_found=org.split(",")[0]
            ht[line_found]=0
            outfile.write(line+"\t\""+line_found+"\"\tNone\n")
            outfile.flush()
    infile.close()
    outfile.close()
    print len(ht)

    # connection.close()
